package com.atguigu.webapp.config;

import com.atguigu.webapp.converter.StringToBaseEnumConverterFactory;
import com.atguigu.webapp.interceptor.AuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author 梅超凡
 * @Date 2024/11/20~17:26
 * @Description 拦截器配置类
*/
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Value("${app.auth.path-patterns.include}")
    private String[] includePathPatterns;

    @Value("${app.auth.path-patterns.exclude}")
    private String[] excludePathPatterns;

    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(this.authenticationInterceptor)
                .addPathPatterns(includePathPatterns)
                .excludePathPatterns(excludePathPatterns);
    }

    @Autowired
    private StringToBaseEnumConverterFactory stringToBaseEnumConverterFactory;


    /**
     * @Author 梅超凡
     * @Date 2024/11/14~11:15
     * @Description 将自定义的枚举类型转换工厂类通过格式化注册类添加到转换器工厂内
    */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverterFactory(this.stringToBaseEnumConverterFactory);
    }
}